package com.hb.hb_attention;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;

/**
 * Data access object (DAO) for domain model class HbAttention.
 * @see com.hb.hb_attention.HbAttention
 * @author MyEclipse - Hibernate Tools
 */
public class HbAttentionDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(HbAttentionDAO.class);

	//property constants
	public static final String NEEDID = "needid";
	public static final String USERID = "userid";

	Transaction tx=null;
	
    public void save(HbAttention transientInstance) {
        log.debug("saving HbAttention instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(HbAttention persistentInstance) {
        log.debug("deleting HbAttention instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public HbAttention findById( java.lang.Integer id) {
        log.debug("getting HbAttention instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbAttention instance = (HbAttention) getSession()
                    .get("com.hb.hb_attention.HbAttention", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(HbAttention instance) {
        log.debug("finding HbAttention instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_attention.HbAttention")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding HbAttention instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from HbAttention as model where model." 
         						+ propertyName + "= ?";
         tx=getSession().getTransaction();
     	 tx.begin();
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByNeedid(Object needid) {
		return findByProperty(NEEDID, needid);
	}
	
	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}
	
    public HbAttention merge(HbAttention detachedInstance) {
        log.debug("merging HbAttention instance");
        try {
        	tx=getSession().getTransaction();
        	 tx.begin();
            HbAttention result = (HbAttention) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(HbAttention instance) {
        log.debug("attaching dirty HbAttention instance");
        try {
        	tx=getSession().getTransaction();
        	 tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(HbAttention instance) {
        log.debug("attaching clean HbAttention instance");
        try {
        	tx=getSession().getTransaction();
       	    tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}